//
//  progrossView.m
//  进度条
//
//  Created by wu yong on 16/10/17.
//  Copyright © 2016年 wu yong. All rights reserved.
//
#import "Utils.h"
#import "progrossView.h"
#import <QuartzCore/QuartzCore.h>


@interface progrossView()

@property(nonatomic,strong)UILabel *label;
@property(nonatomic,assign)NSInteger ProgressWidth;
@property(nonatomic,assign)NSInteger Radius;
@end

@implementation progrossView



-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if ([DeviceTypeJudge isPadDeviceType]) {
            self.ProgressWidth = 4;
            self.Radius = 220/2-self.ProgressWidth;
        }else{
            self.ProgressWidth = 2;
            self.Radius = 150*KHeightScale/2-self.ProgressWidth;
        }
         [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(skinSwitchHandler) name:@"skinswitch" object:nil];
        self.backgroundColor = [Utils colorWithKey:@"mainBgColor"];
    }
    return self;
}
- (UILabel *)label
{
    if (_label == nil) {
        UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
        label.textAlignment = NSTextAlignmentCenter;
        if ([DeviceTypeJudge isPadDeviceType]) {
            label.font = [UIFont boldSystemFontOfSize:42];
        }else{
            label.font = [UIFont boldSystemFontOfSize:24];
        }
        
        label.textColor = [Utils UIColorFromHex:0Xbdd5f4];
        [self addSubview:label];
        _label = label;
    }
    return _label;
}

- (void)setProgress:(NSInteger)progress
{
    _progress = progress;
    self.label.text = [NSString stringWithFormat:@"%2ld%%",progress];
    [self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
    // 1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //    //绘制环形进度环
    CGFloat start = M_PI/5;
    CGFloat pro = (CGFloat)self.progress /100;
    CGFloat to = start+ pro * M_PI * 2; // - M_PI * 0.5为改变初始位置
    CGFloat xCenter = rect.size.width * 0.5;
    CGFloat yCenter = rect.size.height * 0.5;
    
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path addArcWithCenter:CGPointMake(xCenter,yCenter) radius:self.Radius startAngle:start endAngle:to clockwise:YES];
    CGContextSetStrokeColorWithColor(ctx, [Utils colorWithKey:@"circleColor"].CGColor);
    // 3.把路径添加到上下文
    CGContextSetLineWidth(ctx, self.ProgressWidth);
    CGContextAddPath(ctx, path.CGPath);
    // 4.把上下文渲染到视图
    CGContextStrokePath(ctx);

}

- (void)skinSwitchHandler{
    self.backgroundColor = [Utils colorWithKey:@"mainBgColor"];
}
@end
